package com.esun.cheetah.view.setting;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.view.setting.com.SetCommu;
import com.esun.util.Tools;

/**
 * 用户选择时间段 com.esun.cheetah.view.setting.TimeAct
 * 
 * @author 任付江 create at 2012-3-30 上午10:41:31
 */
public class TimeAct extends Activity {
	/** 开始时间EditText */
	private EditText etBeginTime;
	/** 结束时间EditText */
	private EditText etEndTime;
	/** 总开关CheckBox */
	private CheckBox ckSwitchTime;
	/** 开始时间标志位 */
	private boolean isbgTime;
	/** 结束时间标志位 */
	private boolean isEndTime;
	/** 返回按钮 */
	private Button btnBack;
	/** 读取preference里面的时间 */
	private SharedPreferences sharedPrefs;
	/** 开始时间 */
	private String beginTime;
	/** 结束时间 */
	private String endTime;
	/** 用户记住用户是否已经更改设置 */
	private int alter = 0;
	/** 开关 */
	private boolean isONOROFF = true;
	Handler timeHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_time);
		initView();
	}

	// 实例化组件
	private void initView() {
		sharedPrefs = this.getSharedPreferences(Config.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		beginTime = sharedPrefs.getString(Config.BEGIN_TIME, "00:00");
		endTime = sharedPrefs.getString(Config.END_TIME, "23:59");
		isONOROFF = sharedPrefs.getBoolean(Config.IS_CHOOSE_TIME, true);
		btnBack = (Button) findViewById(R.id.btnCustomBack);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		etBeginTime = (EditText) findViewById(R.id.etBeginTime);
		etBeginTime.setText(beginTime);
		etBeginTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isONOROFF) {
					isbgTime = true;
					isEndTime = false;
					alter = 1;
					createDialog("选择开始时间");
				}
			}
		});
		etEndTime = (EditText) findViewById(R.id.etEndTime);
		etEndTime.setText(endTime);
		etEndTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isONOROFF) {
					isEndTime = true;
					isbgTime = false;
					alter = 1;
					createDialog("选择结束时间");
				}
			}
		});

		ckSwitchTime = (CheckBox) findViewById(R.id.ckSwitchTime);
		ckSwitchTime.setChecked(isONOROFF);
		ckSwitchTime.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isONOROFF = isChecked;
				sharedPrefs.edit().putBoolean(Config.IS_CHOOSE_TIME, isChecked).commit();
				alter = 1;
				if (!isChecked) {
					etEndTime.setClickable(true);
					etBeginTime.setClickable(true);
				} else {
					etEndTime.setClickable(false);
				}
			}
		});
	}

	/**
	 * 时间选择对话框
	 */
	public void createDialog(String promp) {
		final Dialog inputPwdDialog = new Dialog(this, R.style.dialog);
		inputPwdDialog.getWindow().setContentView(R.layout.time_choose_dialog);
		TextView tvPromp = (TextView) inputPwdDialog.findViewById(R.id.tvChooseTimeTip);
		tvPromp.setText(promp);
		ListView lvTime = (ListView) inputPwdDialog.getWindow().findViewById(R.id.lvTime);
		Button btnCacel = (Button) inputPwdDialog.getWindow().findViewById(R.id.btnCancel);
		btnCacel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inputPwdDialog.cancel();
			}
		});
		TimeAdapter timeAdapter = new TimeAdapter(TimeAct.this, inputPwdDialog);
		lvTime.setAdapter(timeAdapter);
		inputPwdDialog.show();
	}

	// 捕获back键
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			if (Tools.networkStatus(this)) {
				new SetCommu(timeHandler, TimeAct.this, etBeginTime.getText().toString(), etEndTime.getText().toString());
				changeValues(Config.BEGIN_TIME, etBeginTime.getText().toString());
				changeValues(Config.END_TIME, etEndTime.getText().toString());
			} else {
				Tools.TipNetworkError(TimeAct.this, Language.INTERNET_FAIL);
			}

			this.finish();
			break;
		default:
			break;
		}
		return super.dispatchKeyEvent(event);
	}

	/**
	 * 时间适配器 com.esun.cheetah.view.setting.TimeAdapter
	 * 
	 * @author 任付江 create at 2012-3-30 上午11:01:05
	 */
	public class TimeAdapter extends BaseAdapter {
		/** 上下文对象 */
		private Context context = null;
		/** 时间数组 */
		private String[] time = { "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00",
				"11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00",
				"23:59" };
		/** 时间对话框 */
		private Dialog inputPwdDialog;

		public TimeAdapter(Context c, Dialog inputPwdDialog) {
			this.context = c;
			this.inputPwdDialog = inputPwdDialog;
		}

		@Override
		public int getCount() {
			return time.length;
		}

		@Override
		public Object getItem(int position) {
			return time[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater lif = LayoutInflater.from(context);
			convertView = lif.inflate(R.layout.time_ckview, null);
			final CheckedTextView ctb = (CheckedTextView) convertView.findViewById(R.id.chtvTime);
			beginTime = sharedPrefs.getString(Config.BEGIN_TIME, "00:00");
			endTime = sharedPrefs.getString(Config.END_TIME, "23:59");
			final String strTime = time[position];
			if (isbgTime && beginTime.equals(strTime) && etBeginTime.getText().toString().equals(strTime)) {
				ctb.setCheckMarkDrawable(context.getResources().getDrawable(R.drawable.btn_focused_popup_radiobtn_32x32));
				changeValues(Config.BEGIN_TIME, strTime);
			}
			if (isEndTime && strTime.equals(endTime) && etEndTime.getText().toString().equals(strTime)) {
				ctb.setCheckMarkDrawable(context.getResources().getDrawable(R.drawable.btn_focused_popup_radiobtn_32x32));
				changeValues(Config.END_TIME, strTime);
			}
			ctb.setText(strTime);
			ctb.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					ctb.setCheckMarkDrawable(context.getResources().getDrawable(R.drawable.btn_focused_popup_radiobtn_32x32));
					if (isbgTime && !isEndTime) {
						changeValues(Config.BEGIN_TIME, strTime);
						etBeginTime.setText(strTime);
						isbgTime = false;
					}
					if (isEndTime) {
						changeValues(Config.END_TIME, strTime);
						isEndTime = false;
						etEndTime.setText(strTime);
						isbgTime = false;
					}
					inputPwdDialog.cancel();
				}
			});
			return convertView;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (alter == 1&&isONOROFF) {
			new SetCommu(timeHandler, TimeAct.this, sharedPrefs.getString(Config.BEGIN_TIME, "00:00"), sharedPrefs.getString(
					Config.END_TIME, "00:00")).startCommunicate();
			promp("设置已保存");
		}
		if(!isONOROFF){
			promp("已关闭提示");
		}
		alter = 1;
	}

	/**
	 * 保存改变后的参数到preferences
	 * 
	 * @param key
	 * @param values
	 */
	private void changeValues(String key, String values) {
		Editor editor = sharedPrefs.edit();
		editor.putString(key, values);
		editor.commit();
	}
	private void promp(String str){
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
}
